﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Partial Public Class ModificationHistory
#Region "Constants"
    Private Const MAX_ROWS As Integer = 200
#End Region
#Region "Public Section"
    Public Shared Function RetrieveAll() As IEnumerable(Of ModificationHistory)
        Dim db As EquipmentDataContext = Database.Instance
        Return db.ModificationHistories.OrderBy(Function(h) h.ActionDate)
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As ModificationHistory
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From h In db.ModificationHistories Where h.ID = id
        Return result.FirstOrDefault()
    End Function

    Public Shared Function RetrieveAllByEntityTypeAndID(ByVal type As EntityType, ByVal entityID As Integer) As IEnumerable(Of ModificationHistory)
        Return From h In Database.Instance.ModificationHistories Where h.EntityType = type AndAlso h.EntityID = entityID Order By h.ActionDate Descending
    End Function

    Public Shared Function RetrieveAllByEntityType(ByVal type As EntityType) As IEnumerable(Of ModificationHistory)
        Return From h In Database.Instance.ModificationHistories Where h.EntityType = type Order By h.ActionDate Descending
    End Function

    Public Shared Function RetrievePage(ByVal type As EntityType, ByVal entityID As Integer, ByVal pageNo As Integer) As IEnumerable(Of ModificationHistory)
        Return RetrieveAllByEntityTypeAndID(type, entityID).Skip((pageNo - 1) * MAX_ROWS).Take(MAX_ROWS).OrderByDescending(Function(h) h.ActionDate)
    End Function

    Public Shared Function RetrievePage(ByVal type As EntityType, ByVal pageNo As Integer) As IEnumerable(Of ModificationHistory)
        Return RetrieveAllByEntityType(type).Skip((pageNo - 1) * MAX_ROWS).Take(MAX_ROWS).OrderByDescending(Function(h) h.ActionDate)
    End Function

    Public Shared Sub Insert(ByVal type As EntityType, ByVal name As String, ByVal userID As Integer, ByVal entityID As Integer)
        Insert(type, name, ModificationAction.Created, userID, String.Empty, entityID, 0)
    End Sub

    Public Shared Sub Insert(ByVal type As EntityType, ByVal name As String, ByVal userID As Integer, ByVal entityID As Integer, ByVal siteID As Integer)
        Insert(type, name, ModificationAction.Created, userID, String.Empty, entityID, siteID)
    End Sub

    Public Shared Sub Insert(ByVal type As EntityType, ByVal name As String, ByVal action As ModificationAction, ByVal userID As Integer, ByVal entityID As Integer, ByVal siteID As Integer)
        Insert(type, name, action, userID, String.Empty, entityID, siteID)
    End Sub

    Public Shared Sub Insert(ByVal type As EntityType, ByVal name As String, ByVal action As ModificationAction, ByVal userID As Integer, ByVal detailList As IEnumerable(Of String), ByVal entityID As Integer)
        Insert(type, name, action, userID, String.Join(", ", detailList), entityID, 0)
    End Sub


    Public Shared Sub Insert(ByVal type As EntityType, ByVal name As String, ByVal action As ModificationAction, ByVal userID As Integer, ByVal detailList As IEnumerable(Of String), ByVal entityID As Integer, ByVal siteID As Integer)
        Insert(type, name, action, userID, String.Join(", ", detailList), entityID, siteID)
    End Sub

    Public Shared Sub Insert(ByVal type As EntityType, ByVal name As String, ByVal action As ModificationAction, ByVal userID As Integer, ByVal detail As String, ByVal entityID As Integer)
        Insert(type, name, action, userID, detail, entityID, 0)
    End Sub


    Public Shared Sub Insert(ByVal type As EntityType, ByVal name As String, ByVal action As ModificationAction, ByVal userID As Integer, ByVal detail As String, ByVal entityID As Integer, ByVal siteID As Integer)
        Dim db As EquipmentDataContext = Database.Instance

        Dim historyItem As New ModificationHistory

        historyItem.Action = action
        historyItem.ActionDate = DateTime.Now.ToUniversalTime()
        historyItem.ModifiedByUserID = userID
        historyItem.Details = detail
        historyItem.Name = name
        historyItem.EntityType = type

        If entityID <> 0 Then
            historyItem.EntityID = entityID
        Else
            historyItem.EntityID = New Nullable(Of Integer)
        End If

        If siteID <> 0 Then
            historyItem.SiteID = siteID
        Else
            historyItem.SiteID = New Nullable(Of Integer)
        End If

        db.ModificationHistories.InsertOnSubmit(historyItem)

        Try
            db.SubmitChanges()

        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

    End Sub
#End Region

End Class
